import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '@/layout/Login.vue'
import Layout from '@/layout/Layout.vue'

const baseRoute = {
  path: '/',
  name: 'Page',
  component: Layout,
  children: [
    {
      path: '/dataStatistics',
      name: 'DataStatistics',
      permission: true,
      component: () => import('@/views/DataStatistics/index.vue'),
    },
    {
      path: '/role',
      name: 'Permission',
      permission: true,
      component: () => import('@/views/Role/index.vue'),
    },
    {
      path: '/dept',
      name: 'dept',
      permission: true,
      component: () => import('@/views/Dept/index.vue'),
    },
    {
      path: '/clue',
      name: 'clue',
      permission: true,
      component: () => import('@/views/Clue/index.vue'),
    },
    {
      path: '/publicClue',
      name: 'publicClue',
      permission: true,
      component: () => import('@/views/PublicClue/index.vue'),
    },
    {
      path: '/propertycompany',
      name: 'PropertyCompany',
      permission: true,
      component: () => import('@/views/propertycompany/index.vue'),
    },
    {
      path: '/arrears',
      name: 'Arrears',
      permission: true,
      component: () => import('@/views/Arrears/index.vue'),
    },
    {
      path: '/litigation',
      name: 'Litigation',
      permission: true,
      component: () => import('@/views/Litigation/index.vue'),
    },
    {
      path: '/court',
      name: 'Court',
      permission: true,
      component: () => import('@/views/Court/index.vue'),
    },
    {
      path: '/wyView',
      name: 'WyView',
      permission: true,
      component: () => import('@/views/WyView/index.vue'),
    },
  ]
}

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login,
    },
  ]
})

export const addPermissionRoute = (arr) => {
  const permissionMap = arr.reduce((map, v) => {
    map[v.name] = true
    return map
  }, {})

  baseRoute.children = baseRoute.children.filter((v) => {
    if (v.permission) {
      return permissionMap[v.name]
    } else {
      return true
    }
  })
  baseRoute.redirect = baseRoute.children[0]?.path || ''
  router.addRoute(baseRoute)
}

export default router
